import { getCuesTopicPage, updateTopicManageRecordState } from "../../../../../service/api/api-cues-topic";
import { calculateDaysAgo } from "../../../../../utils/util";

// components/examineTopic/index.ts
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    access: {} as any,// 当前用户权限
    topicList: [],//选题审批列表
    handleName: '',//操作名称
    handleContent: '',//操作提示描述
    handelDescribes: '',//操作描述
    handelTopicId: "",//操作的线索的id
    buttonRecordState: 0,//操作按钮状态
    buttonPublishGrade: 1,//发布弹窗选择的等级
    grade: 1,//分类
    currentPage: 1, // 当前页数
    pageSize: 8, // 每页数据数量
    hasMoreData: true, // 是否还有更多数据
    total: 0,//所有数据条数
    mode: 3,//选题审批
    firstReachBottom: 0,//是否第一次触底
    // searchValue: '',//搜索框的文字,
  },
  // pageLifetimes: {
  //   show: function () {
  //     // 页面被展示
  //     this.getTopicList({ current: this.data.currentPage, pageSize: this.data.pageSize, mode: this.data.mode })
  //   },
  // },
  lifetimes: {
    attached: function () {
      const { globalData: { access } } = getApp();
      this.setData({
        access,
      })
      this.getTopicList({ current: this.data.currentPage, pageSize: this.data.pageSize, mode: this.data.mode })
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // onTag1change(e) {
    //   console.log('=====', e.detail.checked);
    // },
    // changeHandle(e) {
    //   const { value } = e.detail;
    //   this.setData({
    //     searchValue: value,
    //   });
    // },
    // blurHandle() {
    //   this.getTopicList({ mode: this.data.mode, current: this.data.currentPage, pageSize: this.data.pageSize, name: this.data.searchValue })
    // },
    // actionHandle() {
    //   this.getTopicList({ mode: this.data.mode, current: this.data.currentPage, pageSize: this.data.pageSize })
    //   this.setData({
    //     searchValue: '',
    //   });
    // },
    // 下拉刷新
    onPullDownRefresh() {
      this.getTopicList({ current: 1, pageSize: this.data.pageSize, mode: 2 })
        .then(() => {
          wx.stopPullDownRefresh();
        })
        .catch(() => {
          wx.stopPullDownRefresh();
        });
    },
    // 下拉加载更多数据
    scrollBottom() {
      this.setData({
        firstReachBottom: this.data.firstReachBottom + 1
      })
      if (this.data.total > this.data.pageSize) {
        wx.showLoading({
          title: '加载中...'
        })
        if (this.data.hasMoreData) {
          const nextPage = this.data.currentPage + 1;
          this.setData({
            currentPage: nextPage,
          })
          this.getTopicList({ mode: this.data.mode, current: nextPage, pageSize: this.data.pageSize })
          wx.hideLoading()
        } else {
          if (this.data.firstReachBottom === this.data.currentPage) {
            wx.showToast({
              title: '已加载完成'
            })
          }
          wx.hideLoading()
        }
      }
    },
    //获取选题列表
    async getTopicList(params: { current: number, pageSize: number, mode: number, name: string }) {
      wx.showLoading({
        title: '加载中...'
      })
      try {
        const response = await getCuesTopicPage({ current: params.current, pageSize: params.pageSize, mode: params.mode, name: params.name });
        const hasMoreData = response.data.total >= params.pageSize * params.current;
        const list = response.data.data
        list.forEach(item => {
          item.beforNow = calculateDaysAgo(item.createdTime)
        })
        this.setData({
          topicList: [...this.data.topicList, ...list],
          total: response.data.total,
          hasMoreData
        })
        wx.hideLoading()
      } catch (error) {
        wx.hideLoading()
      }
    },

    //改变分类
    handleChangeGrade(e) {
      this.setData({
        grade: e.detail.value,
        buttonPublishGrade: e.detail.value
      })
    },
    //打开操作描述框
    openDescribes() {
      this.setData({
        showDescribes: true
      })
    },
    //关闭操作描述框
    closeDescribes() {
      this.setData({
        showDescribes: false,
      })
    },
    //操作描述框确认
    async confirmDescribes(e) {
      // if (this.data.topicDetails && this.data.topicDetails.recordState == 0) {
      //   return;
      // }
      const response = await updateTopicManageRecordState({
        // id: this.data.topicId,
        recordState: this.data.buttonRecordState,
        updateSource: 1,
        grade: this.data.grade,
        recordDescribes: this.data.handelDescribes,
        id: this.data.handelTopicId
      });
      if (response.code === 0) {
        wx.showToast({
          title: '提交成功',
          icon: "none"
        })
        const pages = getCurrentPages() //获取页面列表
        const perpage = pages[pages.length - 1] //当前页
        perpage.onLoad({ mode: 3 })
        this.closeDescribes()
        return true;
      } else {
        this.closeDescribes()
        return false;
      }

    },
    //废弃操作
    handlWaste(e) {
      this.setData({
        handleContent: e.currentTarget.dataset.content,
        handleName: e.currentTarget.dataset.name,
        handelDescribes: '',
        buttonRecordState: 4,
        handelTopicId: e.currentTarget.dataset.topicId
      })
      this.openDescribes()
    },
    //退回操作
    handleBack(e) {
      this.setData({
        handleContent: e.currentTarget.dataset.content,
        handleName: e.currentTarget.dataset.name,
        handelDescribes: '',
        buttonRecordState: 2,
        handelTopicId: e.currentTarget.dataset.topicId
      })
      this.openDescribes()
    },
    //发布操作
    handelPublish(e) {
      this.setData({
        handleContent: e.currentTarget.dataset.content,
        handleName: e.currentTarget.dataset.name,
        handelDescribes: '',
        buttonRecordState: 3,
        handelTopicId: e.currentTarget.dataset.topicId,
        buttonPublishGrade: e.currentTarget.dataset.grade
      })
      this.openDescribes()
    },
    //输入操作描述
    onDescribInput(e) {
      this.setData({
        handelDescribes: e.detail.value
      })
    },
    //跳转详情页面
    handelDetail(e) {
      wx.navigateTo({
        url: '/pages/cues/topic/details/index?topicId=' + e.currentTarget.dataset.id + '&mode=' + this.data.mode
      })
    },
  }
})
